<template>
  <div class="cesium-viewer-wapper">
    <div id="cesium-viewer" />
    <div class="tool-box-left-top">
      <p><button :class="{ active: !!synchronizer }" @click="handleClick('synchronizer')">同步</button></p>
      <p><button :class="{ active: isRepeating }" @click="handleClick('isRepeating')">重复</button></p>
      <p><button :class="{ active: videoOverlay }" @click="handleClick('videoOverlay')">播放器</button></p>
    </div>
    <video ref="trailer" id="trailer" muted autoplay loop crossorigin controls v-show="videoOverlay">
      <source src="https://cesium.com/public/SandcastleSampleData/big-buck-bunny_trailer.webm" type="video/webm" />
      <source src="https://cesium.com/public/SandcastleSampleData/big-buck-bunny_trailer.mp4" type="video/mp4" />
      <source src="https://cesium.com/public/SandcastleSampleData/big-buck-bunny_trailer.mov" type="video/quicktime" />
      Your browser does not support the
      <code>video</code>
      element.
    </video>
    <viewerSettingSidebar :viewerSource="viewerSource" style="top:160px" />
    <MousePosition show :viewerSource="viewerSource" />
    <g3vPlayer showTime :clockOptions="clockOptions" :viewerSource="viewerSource" :sliderOption="sliderOption" v-model:playing="playing" />
  </div>
</template>

<script>
import * as Cesium from '@cesiumjs';
import { defineComponent, ref, watch } from 'vue';
import { SelectBox, SliderBar } from '@components/widgets';
import { G3V } from '@g3x';
import { RainEffect, SnowEffect, FogEffect } from '@g3x/g3v/effects';
import { MousePosition, viewerSettingSidebar, g3vPlayer } from '@g3x/g3v/ui';

const g3vOptions = {
  defaultViewRectangle: G3V.defaultViews.global.china.rectangle,
  viewers:[{
    dom: "cesium-viewer",
    name: "viewer1",
    title: "viewer#01",
    viewer:{
      imagery: { type: 'arcgisImagery', options: { brightness: 1, show: true } },
      imageryProvider: null,
      terrain: { type: 'default', options: { requestVertexNormals: true, requestWaterMask: true } },
      terrainProvider: null,
      shadows: false,
    },
    layers:[
      // { type: 'tdtIboMap', options: { brightness: 1, show: true } },
      // { type: 'tdtImgAnno', options: { brightness: 1, show: true } },
      // { type: 'darkMask', options: { brightness: 1, alpha: 0.85 } },
      { type: 'graticule', options: { brightness: 1, show: true } }
    ],
    globe: {
      depthTestAgainstTerrain: true,
      enableLighting: true //启用以太阳为光源的地球
    },
    navigation: {
      // defaultResetView: G3V.defaultViews.global.china.rectangle ,
      defaultResetView: null //flyHome
    },
    hideLayers: false,
  }],
};

export default defineComponent({
  name: 'my-video', // 视频
  components: { SelectBox, SliderBar, viewerSettingSidebar, g3vPlayer, MousePosition },
  setup() {
    return {};
  },
  props: {},
  data() {
    return {
      playing: true,
      g3v: null,
      drawingMode: '',
      // timeStart: '2021-05-03T00:00:00+08:00',
      // timeEnd: '2021-05-09T00:00:00+08:00',
      sliderOption: { min: 0, max: 3600 * 24, step: 1 },
      synchronizer: null,
      isRepeating: true,
      videoOverlay: false
    };
  },
  computed: {
    viewer() {
      return this.g3v && this.g3v.viewer$1;
    },
    viewerSource() {
      const { viewer } = this;
      return { viewer };
    },
    clockOptions() {
      return { multiplier: 1 };
    }
  },
  watch: {
    drawingMode(val) {
      const { viewer } = this;
      if (!viewer) return;
    }
  },
  mounted() {
    console.log('my-video mounted');
    this.initViewer();
  },
  beforeUnmount() {},
  unmounted() {
    this.g3v && this.g3v.destroy();
    this.g3v = null;
  },
  methods: {
    initViewer() {
      let vm = this;
      vm.g3v = new G3V(g3vOptions,()=>{
        vm.onViewerInited();
        vm.initEffects();
      });
    },
    onViewerInited() {},
    initEffects() {
      let vm = this;
      const viewer = this.viewer;
      var videoElement = vm.$refs.trailer;
      var sphere = viewer.entities.add({
        position: Cesium.Cartesian3.fromDegrees(-79, 39, 1000),
        ellipsoid: {
          radii: new Cesium.Cartesian3(1000, 1000, 1000),
          material: videoElement
        }
      });

      sphere.ellipsoid.material.repeat = new Cesium.CallbackProperty(function(time, result) {
        if (!Cesium.defined(result)) {
          result = new Cesium.Cartesian2();
        }
        if (vm.isRepeating) {
          result.x = 8;
          result.y = 8;
        } else {
          result.x = 1;
          result.y = 1;
        }
        return result;
      }, false);

      viewer.trackedEntity = sphere;

      viewer.camera.flyTo({
        duration: 4,
        destination: Cesium.Cartesian3.fromDegrees(-79.00326, 38.95259, 3391),
        orientation: {
          heading: 0.08309,
          pitch: -0.42314,
          roll: 0.00019
        },
        complete(){
        }
      });

      var videoElement = this.$refs.trailer;
      videoElement.play();
    },
    handleClick(value) {
      const viewer = this.viewer;
      var videoElement = this.$refs.trailer;
      switch (value) {
        case 'isRepeating':
          this.isRepeating = !this.isRepeating;
          break;
        // Like Image, the video element doesn't have to be part of the DOM or
        // otherwise on the screen to be used as a texture.
        case 'videoOverlay':
          this.videoOverlay = !this.videoOverlay;
          // videoElement.style.display = this.videoOverlay ? '' : 'none';
          videoElement.play();
          break;
        case 'synchronizer':
          // By default, the video plays normally and simply shows whatever frame the video is currently on.
          // We can synchronize the video with the scene clock using a VideoSynchronizer.
          if (this.synchronizer) {
            this.synchronizer = this.synchronizer.destroy();
            videoElement.playbackRate = 1.0;
            videoElement.play();
            return;
          }

          this.synchronizer = new Cesium.VideoSynchronizer({
            clock: viewer.clock,
            element: videoElement
          });
          videoElement.play();
          break;
      }
    }
  }
});
</script>

<style lang="less">
.tool-box-left-top {
  display: flex;

  button {
    min-width: 5em;
    background-color: rgba(0, 0, 0, 0.75);
    border-color: rgba(0, 0, 0, 0);
    cursor: pointer;

    transition: all 500ms;

    &:hover {
      opacity: 0.75;
    }

    &.active {
      background-color: rgba(0, 20, 200, 0.75);
    }
  }
}

#trailer {
  position: absolute;
  bottom: 60px;
  right: 5px;
  width: 320px;
  height: 180px;
}
</style>
